from rest_framework import status
from rest_framework.test import APITestCase
from django.test import TransactionTestCase
from django.urls import reverse
from .models import Student


class StudentViewSetTestCase(TransactionTestCase):
    def setUp(self):
        # 可选：如果接口需要认证
        # from django.contrib.auth.models import User
        # self.user = User.objects.create_user(username='testuser', password='testpass')
        # self.client.force_authenticate(user=self.user)

        # 初始化测试数据
        self.student_data = {
            'name': 'Test Student',
            'age': 20,
            'gender': 'M',
            'email': 'test@example.com',
            'phone': '66667777888'
        }
        self.student = Student.objects.create(**self.student_data)

    def test_create_student(self):
        url = reverse('student-list')
        new_data = {
            'name': 'New Student',
            'age': 22,
            'gender': 'F',
            'email': 'new@example.com',
            'phone': '44446666888'
        }
        response = self.client.post(url, new_data, format='json')
        print(response,
              response.content,
              sep='\n')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Student.objects.count(), 2)
        self.assertEqual(Student.objects.latest('id').name, 'New Student')

    def test_list_students(self):
        url = reverse('student-list')
        response = self.client.get(url)
        print(response,
              response.content,
              sep='\n')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        # 如果启用分页，使用 response.data['results']
        self.assertGreaterEqual(len(response.data['results']), 1)
        print(response)

    def test_retrieve_student(self):
        url = reverse('student-detail', args=[self.student.id])
        response = self.client.get(url)
        print(response,
              response.content,
              sep='\n')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['email'], self.student_data['email'])

    def test_update_student(self):
        url = reverse('student-detail', args=[self.student.id])
        updated_data = {
            'name': 'Updated Name',
            'age': 21,
            'gender': 'F',
            'email': 'updated@example.com',
            'phone': '11112222333'
        }
        response = self.client.put(url, updated_data, format='json')
        print(response,
              response.content,
              sep='\n')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.student.refresh_from_db()
        self.assertEqual(self.student.name, 'Updated Name')

    def test_delete_student(self):
        url = reverse('student-detail', args=[self.student.id])
        response = self.client.delete(url)
        print(response,
              response.content,
              sep='\n')
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        self.assertEqual(Student.objects.count(), 0)

    # 可选：测试部分更新
    def test_partial_update_student(self):
        url = reverse('student-detail', args=[self.student.id])
        partial_data = {'age': 21}
        response = self.client.patch(url, partial_data, format='json')
        print(response,
              response.content,
              sep='\n')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.student.refresh_from_db()
        self.assertEqual(self.student.age, 21)
